<template>
	<view class="">
		<view class="force-login-wrap ">
			<view class="force-login__content y-f">
				<!-- <open-data class="user-avatar" type="userAvatarUrl"></open-data> -->
				<!-- <open-data class="user-name" type="userNickName"></open-data> -->
				<view class="login-notice"><text>为了提供更优质的服务，需要获取您的头像昵称</text></view>
				<button class="cu-btn author-btn animation-shake" @click="getWechat" open-type="getUserInfo"><text>授权并查看</text></button>
				<button class="cu-btn close-btn" @tap="closeAuth"><text>暂不授权</text></button>
			</view>
		</view>
		<!-- <view class="force-login-wrap " v-else>
			<view class="force-login__content y-f">
				<open-data class="user-avatar" type="userAvatarUrl"></open-data>
				<open-data class="user-name" type="userNickName"></open-data>
				<view class="login-notice"><text>为了提供更优质的服务，需要获取您的手机号码</text></view>
				<button class="cu-btn author-btn animation-shake" @getphonenumber="getMobileNum" open-type="getPhoneNumber">
					<text>获取手机号码</text>
				</button>
				<button class="cu-btn close-btn" @tap="closeAuth"><text>暂不获取</text></button>
			</view>
		</view> -->
		<u-toast ref="uToast" />
	</view>
</template>

<script>
/**
 * 登录提示页
 * @property {Boolean} value=showLoginTip - 由v-model控制显示隐藏。
 * @property {Boolean} forceOauth - 小程序端特制的全屏登录提示。
 */
var jweixin = require('jweixin-module');
jweixin.ready(function() {
	// TODO
});

export default {
	data() {
		return {};
	},
	methods: {
		async getWechat() {
			let obj = {
				url: encodeURIComponent(window.location.href),
			};
			const res = await this.$u.post('public/wechat_redirect', obj);
			if (!res.code) return;
			location.href = res.data;
		},

		async getMobileNum(e) {
			console.log('e :>> ', e);
			const [err, res] = await uni.login().catch(err => err);
			if (err || res.errMsg !== 'login:ok') return uni.$showMsg('登录失败！');
			const obj = {
				encryptedData: e.detail.encryptedData,
				iv: e.detail.iv,
				code: res.code,
			};

			const ress = await this.$u.post('Index/getUserPhone', obj);
			if (ress.code == 1) {
				console.log('111 :>> ', 111);
				uni.setStorageSync('phone', ress.data.phone);
				this.$u.vuex('vuex_login', 0);
			}
		},
		async closeAuth() {
			this.$u.vuex('vuex_login', 0);
			this.$u.vuex('vuex_showNum', 1);
			uni.removeStorageSync('data');
			uni.removeStorageSync('token');
			uni.removeStorageSync('user');
		},
		async getuserinfo() {
			const from_user_id = uni.getStorageSync('id');
			const [, info] = await uni.getUserProfile({ desc: '用于完善会员资料' });
			const [, res] = await uni.login();
			let obj = {
				code: res.code,
				encrypted_data: info.encryptedData,
				iv: info.iv,
				signature: info.signature,
				avatar: info.userInfo.avatarUrl,
				nickname: info.userInfo.nickName,
			};
			const ress = await this.$u.post('Public/login', obj, {});
			var _this = this;
			if (ress.code == 1) {
				if (from_user_id && ress.data.user.id) {
					this.bindingRelationship(from_user_id, ress.data.user.id);
				}
				uni.setStorageSync('data', ress.data);
				uni.setStorageSync('token', ress.data.token);
				this.$refs.uToast.show({ title: '授权成功', type: 'success' });

				this.$u.vuex('vuex_showNum', 0);
			}
		},
		async bindingRelationship(from_user_id, to_user_id) {
			// from_user_id 邀请人id

			//to_user_id 被邀请人id

			let obj = {
				from_user_id,
				to_user_id,
			};
			const res = await this.$u.post('Public/inviteRegister', obj, {});
		},
	},
};
</script>

<style lang="scss">
// 登录提示
.modal-box {
	width: 610rpx;
	border-radius: 20rpx;
	background: #fff;
	position: relative;
	left: 50%;
	transform: translateX(-50%);
	padding-bottom: 30rpx;

	.head-bg {
		width: 100%;
		height: 210rpx;
	}

	.detail {
		.title1 {
			color: #46351b;
			font-size: 35rpx;
			font-weight: bold;
		}

		.title2 {
			font-size: 28rpx;
			color: #999;
			padding-top: 20rpx;
		}
	}

	.btn-box {
		margin-top: 80rpx;
		.login-btn {
			width: 492rpx;
			height: 70rpx;
			background: linear-gradient(90deg, rgba(233, 180, 97, 1), rgba(238, 204, 137, 1));
			box-shadow: 0px 7rpx 6rpx 0px rgba(229, 138, 0, 0.22);
			border-radius: 35rpx;
			font-size: 28rpx;
			color: rgba(#fff, 0.9);
		}

		.close-btn {
			width: 492rpx;
			height: 70rpx;
			color: #e9b766;
			font-size: 26rpx;
			margin-top: 20rpx;
			background: none;
		}
	}
}

// 小程序登录提醒

.force-login-wrap {
	position: fixed;
	width: 100vw;
	height: 100vh;
	overflow: hidden;
	z-index: 11111;
	top: 0;
	background: linear-gradient(180deg, #f7f7f7 0%, #f7f7f7 98%);
	.logo-bg {
		width: 640rpx;
		height: 300rpx;
	}
	.force-login__content {
		position: absolute;
		left: 50%;
		top: 50%;
		transform: translate(-50%, -50%);
		.user-avatar {
			width: 160rpx;
			height: 160rpx;
			border-radius: 50%;
			overflow: hidden;
			margin-bottom: 40rpx;
		}
		.user-name {
			font-size: 35rpx;
			font-family: PingFang SC;
			font-weight: bold;
			color: #000;
			margin-bottom: 30rpx;
		}
		.login-notice {
			font-size: 28rpx;
			font-family: PingFang SC;
			font-weight: 400;
			color: #000;
			line-height: 44rpx;
			width: 400rpx;
			text-align: center;
			margin-bottom: 80rpx;
		}
		.author-btn {
			width: 630rpx;
			height: 80rpx;
			background: linear-gradient(180deg, #ffffff, #ffffff);
			box-shadow: 0px 7rpx 6rpx 0px #ffffff;
			border-radius: 40rpx;
			font-size: 30rpx;
			font-family: PingFang SC;
			font-weight: 500;
			color: #000;
		}
		.author-btn1 {
			width: 630rpx;
			height: 80rpx;
			background: linear-gradient(180deg, #ffffff, #ffffff);
			box-shadow: 0px 7rpx 6rpx 0px #ffffff;
			border-radius: 40rpx;
			font-size: 30rpx;
			font-family: PingFang SC;
			font-weight: 500;
			color: #000;
		}
		.close-btn {
			width: 630rpx;
			height: 80rpx;
			margin-top: 30rpx;
			border-radius: 40rpx;
			border: 2rpx solid #ffffff;
			background: none;
			font-size: 30rpx;
			font-family: PingFang SC;
			font-weight: 500;
			color: #000;
		}
	}
}
</style>
